package com.jh.fcsm.controller.basic;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.basic.BasicAreaCode;
import com.jh.fcsm.beans.basic.vo.BasicAreaCodeVo;
import com.jh.fcsm.beans.sys.vo.SysUserVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.service.basic.BasicAreaCodeService;
import com.jh.fcsm.util.poi.EasyExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区划Controller
 * 
 * @author cjt
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/basic/area")
@Api(value="行政区划", tags ="行政区划")
public class BasicAreaCodeController extends BaseController{
    @Autowired
    private BasicAreaCodeService basicAreaCodeService;

	private static final String PER_PREFIX = "btn:basic:area:";
	
	/**
	 * 新增行政区划
	 *@param basicAreaCode 行政区划数据 json
	 *@return RestApiResponse<?>
	 *@author szx
	 */
	@RepeatSubAnnotation
	@PostMapping("/save")
	@ApiOperation(value = "新增行政区划", notes = "新增行政区划")
	@SystemLogAnnotation(type = "行政区划",value = "新增行政区划")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"save')")
	public RestApiResponse<?> saveBasicAreaCode(@RequestBody BasicAreaCode basicAreaCode) {
		String id = basicAreaCodeService.saveOrUpdateBasicAreaCode(basicAreaCode);
		return RestApiResponse.ok(id);
	}
	
	/**
	 * 修改行政区划
	 *@param basicAreaCode 行政区划数据 json
	 *@return RestApiResponse<?>
	 *@author szx
	 */
	@RepeatSubAnnotation
	@PostMapping("/update")
	@ApiOperation(value = "修改行政区划", notes = "修改行政区划")
	@SystemLogAnnotation(type = "行政区划",value = "修改行政区划")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"update')")
	public RestApiResponse<?> updateBasicAreaCode(@RequestBody BasicAreaCode basicAreaCode) {
		String id = basicAreaCodeService.saveOrUpdateBasicAreaCode(basicAreaCode);
		return RestApiResponse.ok(id);
	}
	
	/**
	 * 批量删除行政区划(判断 关联数据是否可以删除)
	 *@param ids
	 *@return RestApiResponse<?>
	 *@author szx
	 */
	@RepeatSubAnnotation
	@PostMapping("/delete")
	@ApiOperation(value = "批量删除行政区划", notes = "批量删除行政区划")
	@SystemLogAnnotation(type = "行政区划",value = "批量删除行政区划")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"delete')")
	public RestApiResponse<?> deleteBasicAreaCode(@RequestBody List<String> ids) {
		//判断 关联数据是否可以删除
		basicAreaCodeService.deleteBasicAreaCode(ids);
		return RestApiResponse.ok();
	}
	
	/**
	 * 查询行政区划详情
	 *@param id
	 *@return RestApiResponse<?>
	 *@author szx
	 */
	@GetMapping("/findById")
	@ApiOperation(value = "查询行政区划详情", notes = "查询行政区划详情")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"find')")
	public RestApiResponse<?> findById(@RequestParam("id") String id) {
		BasicAreaCode  basicAreaCode=basicAreaCodeService.findById(id);
		return RestApiResponse.ok(basicAreaCode);
	}
	
	/**
	 * 分页查询行政区划
	 *@param basicAreaCodeVo 行政区划 查询条件
	 *@return RestApiResponse<?>
	 *@author szx
	 */
	@PostMapping("/findPageByQuery")
	@ApiOperation(value = "分页查询行政区划", notes = "分页查询行政区划")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"query')")
	public RestApiResponse<?> findPageByQuery(@RequestBody BasicAreaCodeVo basicAreaCodeVo) {
		PageInfo<BasicAreaCode>  basicAreaCode=basicAreaCodeService.findPageByQuery(basicAreaCodeVo);
		return RestApiResponse.ok(basicAreaCode);
	}

	@PostMapping("/selectAllArea")
	@ApiOperation(value = "查询所有", notes = "查询所有")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"find')")
	public RestApiResponse<?> selectAll() {
		List<BasicAreaCode>  basicAreaCode=basicAreaCodeService.selectAll();
		return RestApiResponse.ok(basicAreaCode);
	}

	@PostMapping("/exportExcel")
	@ApiOperation(value = "导出用户", notes = "导出用户")
	@PreAuthorize("hasAuthority('" + PER_PREFIX + "export')")
	public void exportExcel(BasicAreaCodeVo basicAreaCodeVo) {
		basicAreaCodeVo.setPageNum(100);
		PageInfo<BasicAreaCode> area = basicAreaCodeService.findPageByQuery(basicAreaCodeVo);
		EasyExcelUtils.exportExcel(area.getList(), "地区数据", BasicAreaCode.class, "地区数据.xlsx", response);
//		EasyExcelUtils.exportExcel(area.getList(), "地区数据", BasicAreaCode.class, "地区数据.xlsx", response);
	}
}
